#include "back_camera.h"
#include <iltclient.h>
#include "iltdrawprim.h"
#include <ltobjectcreate.h>
#include "..\clientinterfaces.h"
back_camera::back_camera(void):
m_width(640),
m_height(480),
m_reset(false),
m_show(false)
{
	m_x=0.35f;
	m_y=0.0f;
	m_w=0.3f;
	m_h=0.15f;
}

back_camera::~back_camera(void)
{
	if(m_obj) {
		g_pLTClient->RemoveObject(m_obj);
		m_obj= NULL ;
	}
}

back_camera& back_camera::single()
{
	static back_camera _single;
	return _single;


}

void back_camera::init()
{
	uint32 nWidth, nHeight;
	ObjectCreateStruct objCreate;

	//	Get our screen dimensions, for the camera rectangle
	g_pLTClient->GetSurfaceDims(g_pLTClient->GetScreenSurface(), &nWidth, &nHeight);

	//	Initialize our object creation structure
	LTVector temp;
	temp.x=10;
	temp.y=20;
    temp.z=10;
	objCreate.Clear();
	objCreate.m_ObjectType = OT_CAMERA;
	objCreate.m_Pos=temp;
	m_obj= g_pLTClient->CreateObject(&objCreate);

	if (NULL == m_obj)
		throw std::runtime_error("could not create back camera object") ;


	g_pLTClient->SetCameraRect(m_obj, false, 0, 0, nWidth, nHeight);

	//	This is an fov of 90 degrees
	float fFovX = MATH_PI/2.0f;
	float fFovY = (fFovX * nHeight) / nWidth;
	g_pLTClient->SetCameraFOV(m_obj, fFovX, fFovY);
	

	


}

void back_camera::load_screen_size( float wide,float height )
{
	m_width=wide;
	m_height=height;
	m_reset=false;

}

void back_camera::set_pos_size( float x,float y,float w,float h )
{
	m_x=x;
	m_y=y;
	m_w=w;
	m_h=h;
	m_reset=false;

}

void back_camera::render( float frametime )
{
	if (!m_show)
	{
		return;
	}
	if (!m_reset)
	{
		reset();
		m_reset=true;
    }


  g_pLTClient->ClearScreen(&m_rect,CLEARSCREEN_RENDER,NULL);
  g_pLTClient->RenderCamera(m_obj,frametime);

}

void back_camera::reset()
{
	g_pLTClient->SetCameraRect(m_obj,false,m_x*m_width,m_y*m_height,
		(m_x+m_w)*m_width,(m_y+m_h)*m_height);
	m_rect.left=m_x*m_width;
	m_rect.top=m_y*m_height;
	m_rect.right=(m_x+m_w)*m_width;
	m_rect.bottom=(m_y+m_h)*m_height;

}

void back_camera::set_camera_pos( LTVector* _pos )
{
	g_pLTClient->SetObjectPos(m_obj,_pos);

}

void back_camera::set_camera_rotation( LTRotation* _rotation )
{
	g_pLTClient->SetObjectRotation(m_obj,_rotation);

}

LTVector back_camera::get_camera_pos()
{
	 g_pLTClient->GetObjectPos(m_obj,&m_pos);
	 return m_pos;


}

LTRotation back_camera::get_camera_rot()
{
	g_pLTClient->GetObjectRotation(m_obj,&m_rot);
	return m_rot;

}